﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Vehicle.Web.Pages.Vehicles
{
    /// <summary>
    /// Copyright (C) M-Tear团队
    /// 文 件 名：Vehicles_Add.aspx.cs
    /// 版    本：v1.00.0000
    /// 创建标识：2009-08-28  Created by 杨光远 V1.00.0000
    /// 功能说明：添加车辆信息页面类
    /// 注意事项：无
    /// 
    /// 更新记录：
    /// 
    /// </summary>
    public partial class Vehicles_Add : Component.PageBase
    {

        #region ---基本变量实例化---
        Vehicle.BLL.Vehicles b_Vehicles = new Vehicle.BLL.Vehicles();//定义逻辑层变量
        Vehicle.BLL.Motorcade b_Motorcade = new Vehicle.BLL.Motorcade();//定义逻辑层变量
        BLL.Log b_Log = new Vehicle.BLL.Log();
        Model.VehiclesInfo m_VehiclesInfo = new Vehicle.Model.VehiclesInfo();//实例化实体层
        Vehicle.BLL.Pilot b_Pilot = new Vehicle.BLL.Pilot();//定义逻辑层变量
        #endregion

        #region ---基本方法---
        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                VehiclesDataBind();//绑定数据
            }
        }
        /// <summary>
        /// 初始绑定车队与驾驶员下拉数据
        /// </summary>
        public void VehiclesDataBind()
        {
            b_Motorcade.DropDownListBind(drop_M_ID);//绑定车队
            PilodDataBind();//绑定驾驶员

        }
        /// <summary>
        /// 根据车队绑定驾驶员
        /// </summary>
        public void PilodDataBind()
        {
            b_Pilot.DropDownListBind(drop_P_ID, new Vehicle.Model.ComponentsInfo("Pilot", "M_ID", drop_M_ID.SelectedValue, "V_ID", "0", "P_IsDelete", "1", "P_ID"));//绑定驾驶员

        }
        /// <summary>
        /// 提交数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            //添加前判断是否已经有该车牌号的车辆
            if (b_Motorcade.GetModel(new Vehicle.Model.ComponentsInfo("Vehicles",  "V_LicenseNum", txt_V_LicenseNum.Text.Trim(),"1","1","V_IsDelete", "1", "V_ID")).Rows.Count == 0)
            {
                m_VehiclesInfo.M_ID = Common.TextHelper.StringToInt(drop_M_ID.SelectedValue);//所属车队编号
                m_VehiclesInfo.P_ID = Common.TextHelper.StringToInt(drop_P_ID.SelectedValue);//所属驾驶员编号
                m_VehiclesInfo.V_LicenseNum = txt_V_LicenseNum.Text.Trim();//车牌号
                m_VehiclesInfo.V_ProvinceCity = txt_V_ProvinceCity.Text.Trim();//所属省市
                m_VehiclesInfo.V_Length = txt_V_Length.Text.Trim();//车辆长度
                m_VehiclesInfo.V_Load = Common.TextHelper.StringTofloat(txt_V_Load.Text.Trim());//车辆载重量
                m_VehiclesInfo.V_Model = txt_V_Model.Text.Trim();//车辆类型
                m_VehiclesInfo.V_Route = txt_V_Route.Text.Trim();//车辆路线
                m_VehiclesInfo.V_Unit = txt_V_Unit.Text.Trim();//车辆保险单位
                m_VehiclesInfo.V_Time = DateTime.Now;//车辆录入时间
                m_VehiclesInfo.V_Num = txt_V_Num.Text.Trim();
                m_VehiclesInfo.V_ETiem = Common.TextHelper.StringToDateTime(Gmdp_V_ETiem.DateString);//车辆到期时间
                m_VehiclesInfo.V_Transit = 0;//是否有承运任务 默认0为否
                m_VehiclesInfo.V_Note = Common.TextHelper.CheckStr(txt_V_Note.Text);//车辆信息备注
                m_VehiclesInfo.V_LastTime = DateTime.Now;//车辆最后修改时间默认添加为当前时间
                m_VehiclesInfo.V_IsDelete = 0;//是否删除 默认0 不删除
                m_VehiclesInfo.V_MNumber = this.txt_V_MNumber.Text.Trim();//车辆出厂编号
                m_VehiclesInfo.V_ReleaseDate = Common.TextHelper.StringToDateTime(this.Gmdp_V_ReleaseDate.DateString);//车辆出厂日期
                m_VehiclesInfo.V_BuyDate = Common.TextHelper.StringToDateTime(this.Gmdp_V_BuyDate.DateString);//购买车辆日期
                m_VehiclesInfo.V_Fuel = this.txt_V_Fuel.Text.Trim();//车辆燃料种类
                m_VehiclesInfo.V_Let = this.txt_V_Let.Text.Trim();//车辆排放标准
                m_VehiclesInfo.V_Manufacturer = this.txt_V_Manufacturer.Text.Trim();//车辆生产厂家
                m_VehiclesInfo.V_Color = this.txt_V_Color.Text.Trim();//车辆颜色
                b_Vehicles.Add(m_VehiclesInfo);
                string p_id = b_Pilot.GetModel(new Vehicle.Model.ComponentsInfo("Vehicles", "V_LicenseNum", txt_V_LicenseNum.Text.Trim(), "V_ID")).Rows[0]["V_ID"].ToString();//查出刚刚添加的记录
                b_Vehicles.AllUpdate(new Vehicle.Model.ComponentsInfo("Pilot", "V_ID", p_id, "P_ID", drop_P_ID.SelectedValue));//更新驾驶员表
                BLL.Log b_Log = new Vehicle.BLL.Log();
                b_Log.Add(new Vehicle.Model.LogInfo(session_ID, DateTime.Now, "成功添加编号为" + txt_V_Num.Text.Trim() + "的车辆信息"));//添加日志
                Common.MessageHelper.AlertRedirect("车辆信息添加成功", "Main.aspx");
            }
            else
            {
                Common.MessageHelper.Alert("已经有相同车牌号,请重新填写");
            }
        }

        /// <summary>
        /// 车队下拉菜单选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void drop_M_ID_SelectedIndexChanged(object sender, EventArgs e)
        {
            PilodDataBind();
        }
        #endregion
    }
}
